package com.example.common.quick;

import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.imgload_sdk.ImgLoad;

public class BaseViewHolder extends RecyclerView.ViewHolder {


    private SparseArray<View> sparseArray=new SparseArray<>();
    public BaseViewHolder(@NonNull  View itemView) {
        super(itemView);
    }
    private <V extends View> V find(int id){
        return itemView.findViewById(id);
    }

    public <V extends View> V getView(int id){
        V v;
        v= (V) sparseArray.get(id);
        if (v!=null){
            return v;
        }
        v=find(id);
        if (v!=null){
            sparseArray.put(id,v);
        }
        return v;
    }
    public void setText(int id,String content){
        if (TextUtils.isEmpty(content))
            return;
        TextView textView=getView(id);
        if (textView==null)
            return;
        textView.setText(content);
    }
    public void setVisibility(int id,boolean visibilityFlag){
        View view=getView(id);
        if (view==null)
            return;
        view.setVisibility(visibilityFlag?View.VISIBLE:View.GONE);
    }
    //图片设置
    public void setImage(int id,String path){
        if (TextUtils.isEmpty(path))
            return;
        ImageView imageView = getView(id);
        if (imageView==null){
            return;
        }
//        图片封装
        ImgLoad.loadMatchImg(path,imageView);
    }

    //圆角设置
    public void setcrgImage(int id,String path){
        if (TextUtils.isEmpty(path))
            return;
        ImageView imageView = getView(id);
        if (imageView==null){
            return;
        }
//        图片封装
        ImgLoad.loadCirImg(path,imageView);
    }

    public void errorImage(int id){
        ImageView imageView = getView(id);
        if (imageView==null){
            return;
        }
        ImgLoad.errorimg(imageView);
    }

}
